package cn.xuxiu.bacsic.service.impl;

import cn.xuxiu.bacsic.mapper.BaseMapper;
import cn.xuxiu.bacsic.query.BaseQuery;
import cn.xuxiu.bacsic.service.IBaseService;
import cn.xuxiu.bacsic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true ,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    BaseMapper<T> mapper;

    @Override
    public List<T> loadAll() {
        return mapper.loadAll();
    }

    @Override
    public T loadById(Long id) {
        return mapper.loadById(id);
    }

    @Override
    @Transactional
    public void save(T t) {
        mapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    @Transactional
    public void del(Long id) {
        mapper.del(id);
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        Long totals = mapper.pageCount(query);
        if (totals == 0){
            return new PageList<>();
        }

        List<T> datas = mapper.queryData(query);
        return new PageList<T>(totals,datas);
    }

    @Override
    public void batchRemove(List<Long> ids) {
        mapper.batchRemove(ids);
    }
}
